package com.shanshi.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.shanshi.activity.addressSelector.adapters.ArticleAdapter;
import com.shanshi.activity.response.ArticleContent;
import com.shanshi.activity.response.ArticleTypes;
import com.shanshi.activity.response.IndexInfo;
import com.shanshi.activity.tool.CustomRefreshListView;
import com.shanshi.activity.tool.OkHttpTool;

import java.util.ArrayList;
import java.util.List;

//<<<<<<< HEAD
//=======
//>>>>>>> origin/master

public class SpreadMessage extends MainActivity implements OnClickListener{
	private SharedPreferences sharedPreferences;
	private int nowStoreId;//当前店铺id
	private ImageView Iv;
	private CustomRefreshListView spreadMeagessLv;
	private TextView titleTv;
	private ImageView coverimage1;
	private ListView lv;
	private Button payBt;
	private TextView praiseNum;
	private ImageView spread_location;
	private ImageView spread_suggest;
	private List<ArticleContent.Data> list;
	private ArticleAdapter adapter;
	private int pageIndex=0;
	private boolean isMenu=false;   //是否点击上面的menu事件

	/*******左侧菜单栏********/
	private DrawerLayout drawerLayout;
	private RelativeLayout leftDrawer;
	private ImageView sidebar_avater;
	private TextView sidebar_tel;
	private TextView sidebar_money;
	private LinearLayout sidebar_mywallet;
	private LinearLayout sidebar_billdetails;
	private LinearLayout sidebar_mycollect;
	private LinearLayout sidebar_suggest;
	private LinearLayout sidebar_changeNum;
	private LinearLayout sidebar_exit;
	private boolean state = false;//侧边栏开关状态
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spread_message);

		//获得当前店铺的id
		sharedPreferences = SpreadMessage.this.getSharedPreferences("share", MODE_PRIVATE);
		nowStoreId = sharedPreferences.getInt("nowStoreId", 1);

		Iv = (ImageView) findViewById(R.id.list);
		Iv.setOnClickListener(this);
		praiseNum = (TextView) findViewById(R.id.praiseNum);
		payBt = (Button) findViewById(R.id.PayBt);
		
		payBt.setOnClickListener(this);
		spread_location = (ImageView) findViewById(R.id.spread_location);
		spread_location.setOnClickListener(this);
		spread_suggest = (ImageView) findViewById(R.id.spread_suggest);
		spread_suggest.setOnClickListener(this);
		
		titleTv = (TextView) findViewById(R.id.articleTitle);
		spreadMeagessLv = (CustomRefreshListView) findViewById(R.id.spreadMessage);
		

		list=new ArrayList<ArticleContent.Data>();
		adapter=new ArticleAdapter(this, list);
		spreadMeagessLv.setAdapter(adapter);


		spreadMeagessLv.setOnRefreshListener(new CustomRefreshListView.OnRefreshListener() {

			@Override
			public void onPullRefresh() {
//				Handler handler = new Handler();
//				handler.postDelayed(new Runnable() {
//					@Override
//					public void run() {
						list.clear();
						pageIndex = 0;
						getdatas();
						spreadMeagessLv.completeRefresh();
//					}
//				}, 500);//3秒后执行Runnable中的run方法

			}

			@Override
			public void onLoadingMore() {
				//下拉加载时隐藏底部三个按钮
				payBt.setVisibility(View.GONE);
				spread_location.setVisibility(View.GONE);
				spread_suggest.setVisibility(View.GONE);

				Handler handler = new Handler();
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						pageIndex++;
						isMenu=false;
						getdatas();
						spreadMeagessLv.completeRefresh();
						//加载完成显示底部三个按钮
						payBt.setVisibility(View.VISIBLE);
						spread_location.setVisibility(View.VISIBLE);
						spread_suggest.setVisibility(View.VISIBLE);
					}
				}, 2000);//3秒后执行Runnable中的run方法
			}
		});

		setSlideMenu();
		//是否登录的情况下跳转过来的
		String result = getIntent().getStringExtra("result");
		if(!TextUtils.isEmpty(result)&&!result.equals("error")){
			IndexInfo indexInfo = new Gson().fromJson(result, IndexInfo.class);
			Toast.makeText(this, indexInfo.getStoreId(), Toast.LENGTH_LONG).show();
		}

		//初始化侧边栏
		initSideBar();
	}

	/********初始化侧边菜单栏**********/
	public void initSideBar(){
		drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		leftDrawer = (RelativeLayout) findViewById(R.id.drawer_left);
		sidebar_avater = (ImageView) findViewById(R.id.sidebar_avater);
		sidebar_avater.setOnClickListener(this);

		//加载非会员头像
		if(sharedPreferences.getString("shanShiId","").equals("")){
			Glide.with(SpreadMessage.this).load(R.drawable.name).centerCrop().into(sidebar_avater);
		}else{
			//加载会员头像
			Glide.with(SpreadMessage.this).load(R.drawable.name2).centerCrop().into(sidebar_avater);
		}

		sidebar_tel = (TextView) findViewById(R.id.sidebar_tel);
		//设置电话号码
		String tel = sharedPreferences.getString("phoneNum","139****9636");
		String foreTel = tel.substring(0,3);
		String endTel = tel.substring(7);
		sidebar_tel.setText(foreTel+"****"+endTel);
		sidebar_money = (TextView) findViewById(R.id.sidebar_money);
		//设置余额
		sidebar_money.setText("余额 ￥ "+sharedPreferences.getString("money","120.00"));

		sidebar_mywallet = (LinearLayout) findViewById(R.id.sidebar_mywallet);
		sidebar_mywallet.setOnClickListener(this);
		sidebar_billdetails = (LinearLayout) findViewById(R.id.sidebar_billdetails);
		sidebar_billdetails.setOnClickListener(this);
		sidebar_mycollect = (LinearLayout) findViewById(R.id.sidebar_mycollect);
		sidebar_mycollect.setOnClickListener(this);
		sidebar_suggest = (LinearLayout) findViewById(R.id.sidebar_suggest);
		sidebar_suggest.setOnClickListener(this);
		sidebar_changeNum = (LinearLayout) findViewById(R.id.sidebar_changeNum);
		sidebar_changeNum.setOnClickListener(this);
		sidebar_exit = (LinearLayout) findViewById(R.id.sidebar_exit);
		sidebar_exit.setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()){
			
			case R.id.PayBt:
				//此处要作判断
				String nickName = sharedPreferences.getString("nickName","");
				if(nickName.equals("")){
					intent = new Intent(SpreadMessage.this,CompletePersonalInfoActivity.class);
				}else{
					intent = new Intent(SpreadMessage.this,paymentActivity.class);
				}
				startActivity(intent);

				break;
			case R.id.list:
				if(state){
					drawerLayout.closeDrawer(Gravity.LEFT);

				}else{
					drawerLayout.openDrawer(Gravity.LEFT);
				}
				state = !state;
				break;
			case R.id.spread_location:
				intent = new Intent(SpreadMessage.this,LocationActivity.class);
				startActivity(intent);
				break;
			case R.id.spread_suggest:
				intent = new Intent(SpreadMessage.this,Suggest.class);
				intent.putExtra("lastActivity",2);
				startActivity(intent);
				break;
			case R.id.sidebar_avater:
				intent = new Intent(SpreadMessage.this,MyMessage.class);
				startActivity(intent);
				break;
			case R.id.sidebar_mywallet:
				intent = new Intent(SpreadMessage.this,MyWallet.class);
				startActivity(intent);
				break;
			case R.id.sidebar_billdetails:
				intent = new Intent(SpreadMessage.this,BillDetails.class);
				startActivity(intent);
				break;
			case R.id.sidebar_mycollect:
				intent = new Intent(SpreadMessage.this,Mycollect.class);
				startActivity(intent);
				break;
			case R.id.sidebar_suggest:
				intent = new Intent(SpreadMessage.this,Suggest.class);
				startActivity(intent);
				break;
			case R.id.sidebar_changeNum:
				intent = new Intent(SpreadMessage.this,ChangeTelphone.class);
				startActivity(intent);
				break;
			case R.id.sidebar_exit:
				intent = new Intent(SpreadMessage.this,IndexActivity.class);
				startActivity(intent);
				break;

		}
	}

	public void nextPage(View view){
		Intent intent = new Intent(SpreadMessage.this,StoreInfo.class);
		startActivity(intent);
	}
	
//	public List<Map<String,Object>> getdata(){
//		
//		for(int i=0;i<5;i++){
//			Map<String,Object> map = new HashMap<String, Object>();
////			coverimage1 = (ImageView) findViewById(R.id.coverimage1);
////			coverimage1.setImageResource(R.drawable.lyf3);
//			map.put("coverimage1", R.drawable.lyf3);
//			map.put("articleTitle", "诗和远方");
//			map.put("articleSynopsis","如果你无法简洁的表达你的想法，那只说明你还不够了解它。");
//			map.put("leaveWordNum", 260);
//			map.put("praiseNum", 15);
//			data.add(map);
//		}
//		return data;
//		
//	}
	public void getdatas(){
		if(isMenu){
			list.clear();
		}		
		Log.e("=========sa",""+articleTypeId);
		Log.e("===nowStoreId",nowStoreId+"");
		OkHttpTool.doGet(OkHttpTool.BASE_URL+"articleTypes/"+articleTypeId+"/articleList?userId="+4
				+"&pageIndex="+pageIndex+"&pageSize="+4+"&storeId="+nowStoreId
				, new OkHttpTool.setOkhttpListener() {
			@Override
			public void onSuccess(String s) {
				// TODO Auto-generated method stub
				Gson gson=new Gson();
				ArticleContent content=gson.fromJson(s, ArticleContent.class);
				list.addAll(content.getData());
				Log.e("===dadaqq",s);            //网络请求之后返回的数据，下次遇到这种情况，你先看返回的有没有数据
				Log.e("===dadaqq",list.size()+"");
				handler.sendEmptyMessage(1);
			}
			
			@Override
			public void onFail() {
				// TODO Auto-generated method stub
				Log.e("=========sa","sadfa");
			}
		});
		
	}
	
	/*********为顶部设置滑动菜单栏**********/
	private List<ArticleTypes.Data> artList;
	private void setSlideMenu() {
		//发送请求 
	    OkHttpTool.doGet(OkHttpTool.BASE_URL+"articleTypes?storeId="+1,new OkHttpTool.setOkhttpListener(){
			@Override
			public void onSuccess(String s) {
				// TODO Auto-generated method stub
				Log.e("=========sa",s);
				ArticleTypes articleTypes = new Gson().fromJson(s, ArticleTypes.class);
				artList=articleTypes.getData();
				if (artList.size()>0) {
					articleTypeId=articleTypes.getData().get(0).getId();
				}
	     		handler.sendEmptyMessage(2);
	     		handler.sendEmptyMessage(3);
			}
			@Override
			public void onFail() {
				// TODO Auto-generated method stub
				
			}		
		});
	}
	private int lastIndex=0;
	private int articleTypeId=0;
	private List<ImageView> img=new ArrayList<ImageView>();
	Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				adapter.notifyDataSetChanged();
				break;
			case 2:
				// 包含TextView的LinearLayout
				LinearLayout menuLinerLayout = (LinearLayout) findViewById(R.id.linearLayoutMenu);
				menuLinerLayout.setOrientation(LinearLayout.HORIZONTAL);
				// 参数设置
				LinearLayout.LayoutParams menuLinerLayoutParames = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//				menuLinerLayoutParames.gravity = Gravity.CENTER_HORIZONTAL;
				// 添加TextView控件
				for (int i = 0; i < artList.size(); i++) {
//					final TextView tvMenu = new TextView(SpreadMessage.this);
//					tvMenu.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
//					tvMenu.setPadding(50, 20, 50, 20);
//					tvMenu.setText(artList.get(i).getName());
//					tvMenu.setBackgroundColor(0xFF50B748);
//					tvMenu.setTextColor(Color.WHITE);
//					tvMenu.setTag(i);
//					tvMenu.setGravity(Gravity.CENTER);
					final View inflater=LayoutInflater.from(SpreadMessage.this).inflate(R.layout.menu, null);
					inflater.setTag(i);
					TextView sor=(TextView) inflater.findViewById(R.id.sor);
					sor.setText(artList.get(i).getName());
					final ImageView arrow=(ImageView) inflater.findViewById(R.id.arrow);
					img.add(arrow);
					if (i==0) {
						arrow.setVisibility(View.VISIBLE);
					}
					inflater.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							img.get((Integer)(inflater.getTag())).setVisibility(View.VISIBLE);
							img.get(lastIndex).setVisibility(View.GONE);
							lastIndex=(Integer)(inflater.getTag());
							articleTypeId=artList.get((Integer)(inflater.getTag())).getId();
							isMenu=true;
							pageIndex=0;
							handler.sendEmptyMessage(3);
						}
					});
					menuLinerLayout.addView(inflater, menuLinerLayoutParames);
				}
				break;
			case 3:
				getdatas();
				break;

			default:
				break;
			}
		};
	};

}
